/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public: 
    ListNode* get_mid(ListNode* head)
    {
        ListNode* slow=head,*fast=head;
        while(fast && fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
        }
        return slow;
    }
    ListNode* reverseList(ListNode* head)
    {
        if(head==nullptr || head->next==nullptr)
            return head;
        ListNode* pre=nullptr;
        ListNode* cur=head;
        while(cur)
        {
            ListNode* ne=cur->next;
            cur->next=pre;
            pre=cur;
            cur=ne;
        }
        return pre;
    }
    void reorderList(ListNode* head) {
        ListNode* mid=get_mid(head);
        ListNode* bac_head=mid->next;
        mid->next=nullptr;
        bac_head=reverseList(bac_head);

        ListNode* pre_head=head;
        while(pre_head)
        {
            ListNode* t=pre_head->next;
            if(bac_head)
            {
                pre_head->next=bac_head;
                ListNode* bt=bac_head->next;
                bac_head->next=t;
                bac_head=bt;
            }
            pre_head=t;
        }
    }
};
